package org.cug.service.impl;

import org.cug.mapper.UserMapper;
import org.cug.pojo.User;
import org.cug.service.UserService;
import org.cug.utils.Md5Util;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUserName(String username) {
        User u=userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
            //密码加密
        String md5_password=Md5Util.getMD5String(password);
        //添加数据库
        userMapper.add(username,md5_password);
    }

    @Override
    public void update(User u) {
        u.setUpdateTime(LocalDateTime.now());
        userMapper.update(u);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //本地线程获取
        Map<String,Object> claims=ThreadLocalUtil.get();
        Integer id= (Integer) claims.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> claims=ThreadLocalUtil.get();
        Integer id= (Integer) claims.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}
